package com.lmonkey.servlet.my;

import com.lmonkey.servic.CATAGORYDao;
import com.lmonkey.servic.ORDERDao;
import com.lmonkey.servic.USERDao;
import com.lmonkey.shop.CATEGORY;
import com.lmonkey.shop.PHOTO;
import com.lmonkey.shop.USER;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/shangc/mygxinselect")
public class MyGxinSelect extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //菜单
        ArrayList<CATEGORY> flist = CATAGORYDao.selectcat("father");
        request.setAttribute("flist",flist);
        ArrayList<CATEGORY> clist = CATAGORYDao.selectcat("child");
        request.setAttribute("clist",clist);
        //获取session对象
        HttpSession session = request.getSession();
        String isLogin = (String)session.getAttribute("isLogin");
        USER user = (USER)session.getAttribute("name");
        //判断用户是否登录
        if (user == null || !isLogin.equals("1")){
            PrintWriter out = response.getWriter();
            out.println("<script>alert('请登录');location.href='login.jsp';</script>");
            out.close();
            return;
        }
        //查找头像
        PHOTO hp = (PHOTO)session.getAttribute("hp");
        if (hp != null){
            if (hp.getUser_id() == user.getuser_id()){
                request.setAttribute("hp",hp);
            }else{
                PHOTO p =  USERDao.select(user.getuser_id());
                request.setAttribute("hp",p);
            }
        }else {
            PHOTO p =  USERDao.select(user.getuser_id());
            request.setAttribute("hp",p);
        }
        // 0待支付，1待收货，2已收货，3已关闭,4已收货并且已评价
        int d = ORDERDao.selectDAll(user.getuser_id());
        int h = ORDERDao.selecthAll(user.getuser_id());
        int p = ORDERDao.selectpAll(user.getuser_id());
        request.setAttribute("d",d);
        request.setAttribute("h",h);
        request.setAttribute("p",p);

        request.getRequestDispatcher("mygxin.jsp").forward(request,response);
    }
}
